import { StorageService } from './../../providers/StorageService';
import { UserService } from './../../providers/UserService';
import { BasePage } from './../basePage';
import { SERVER_URL } from './../../providers/Config';
import { Component, ChangeDetectorRef,Injector } from '@angular/core';
import { IonicPage, NavController, NavParams, AlertController, LoadingController } from 'ionic-angular';

declare var $: any;

/**
 * Generated class for the FindpwdPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-findpwd',
  templateUrl: 'findpwd.html',
})
export class FindpwdPage extends BasePage {

  /*登录后台参数loginVo*/
  loginVo = {
    user_phone: '',
    user_pwd: '',
    captcha: '',   /*用于保存用户输入的验证码的msg*/
    guid: ''
  }
  btnName = '发送验证码';
  /*倒计时秒数*/
  nums = 10;
  
  /*后台验证码请求参数*/
  params = {
    phone_number:'',
    captcha_type:0,
  };
  /*验证码按钮是否可以点击*/
  btnIsDisable = true;


  //控制硬件返回按钮是否触发，默认false 
  backButtonPressed: boolean = false;
  //constructor(public navCtrl:NavController,public alertCtrl: AlertController,public navParams: NavParams){}



  constructor(injector:Injector,
    private userservice: UserService,
    public stc:StorageService,
   
  ) {
      super(injector);
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad FindpwdPage');
  }

  /*每次加载 对返回的数据进行处理*/
  ionViewWillEnter() {
    //得到验证码
    // this.changeVerifyCode();
  }

  /*发送验证码*/
  async getCaptchaAPI(){
    var re = /^1[3|4|5|6|7|8|9]\d{9}$/;
    console.log(this.loginVo.user_phone);
    if(this.loginVo.user_phone == ''){
      this.showAlert('','电话号码不能为空');
      return;
    }
    if (!re.test(this.loginVo.user_phone)) {
      this.showAlert('','电话号码格式不正确');
      return;
    }
    /**是否需要从登陆页面传用户的手机号码过来*/ 

    this.params.phone_number = this.loginVo.user_phone;
    this.params.captcha_type = 2;

    let loader = this.loadingCtrl.create({
      content:'',
    });
    loader.present();
    //请求后台登录
    let result = await this.userservice.doGetCaptchaAPI(this.params);
    //let result = await this.userservice.getCaptchaAPI(this.params);
    console.log(result);
    loader.dismiss();
    if(result.code != 1){
      this.showAlert('',result.msg);
      return;
    }else{
      this.showAlert('',result.msg);
      this.nums = 120;
      this.btnIsDisable = false;
      let timeId = setInterval(()=>{
        this.nums--;
        this.btnName = this.nums + '秒后可重新获取';
        if(this.nums < 0){
          clearInterval(timeId);
          this.btnIsDisable = true;
          this.btnName = '点击发送验证码';
          this.nums = 120;
        }
      },1000);    /*没隔一秒执行一次*/
    }
  }
 

  /*修改密码之后 点击确定按钮执行的操作*/
async toLogin(){
    console.log('点击修改密码按钮');
    let re = /^1[3|4|5|6|7|8|9]\d{9}$/;
    let pswre = /^([a-zA-Z0-9]|[!.@#$%^&*]){6,12}$/;
    if(this.loginVo.user_phone == ''){
      this.showAlert('','手机号不能为空');
      return;
    }
    if(!re.test(this.loginVo.user_phone)){
      this.showAlert('','手机号不正确');
      return;
    }
    if(this.loginVo.user_pwd.trim().length == 0){
      this.showAlert('','密码不能为空');
      return;
    }
    if(!pswre.test(this.loginVo.user_pwd.trim())){
      this.showAlert('','密码必须要在6~12位之间');
      return;
    }
    let loader = this.loadingCtrl.create({
      content:'',
    });
    loader.present();
    
    //请求后台登录
    let paramsdo = {
      phone:this.loginVo.user_phone,
      pwd:this.loginVo.user_pwd,
      captcha:this.loginVo.captcha
    }
    console.log(paramsdo);

    /*修改密码的 API*/
    let result =await this.userservice.resetPasswordAPI(paramsdo);
    console.log(result);
    loader.dismiss();
    if(result.code != 1) {
      this.showAlert('',result.msg);
      return;
    }
    this.showAlert('','密码修改成功');
    this.navCtrl.pop();    
  }


  /*提示*/
  showAlert(_title,_msg){
    let alert = this.alertCtrl.create({
      title:_title,
      subTitle:_msg,
      buttons:['确定']
    });
    alert.present();
  }

}
